import type { APIRoute } from 'astro'; export const PUT: APIRoute = async ({ params, request, locals }) => { try { const db = locals.runtime.env.DB; const id = params.id; const body = await request.json(); const { title, description, completed } = body; const { results } = await db.prepare( 'UPDATE tasks SET title = ?, description = ?, completed = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? RETURNING *' ).bind(title, description || '', completed || false, id).all(); if (results.length === 0) { return new Response(JSON.stringify({ error: 'Task not found' }), { status: 404, headers: { 'Content-Type': 'application/json' } }); } return new Response(JSON.stringify(results[0]), { status: 200, headers: { 'Content-Type': 'application/json' } }); } catch (error) { return new Response(JSON.stringify({ error: 'Failed to update task' }), { status: 500, headers: { 'Content-Type': 'application/json' } }); } }; export const DELETE: APIRoute = async ({ params, locals }) => { try { const db = locals.runtime.env.DB; const id = params.id; const { success } = await db.prepare('DELETE FROM tasks WHERE id = ?').bind(id).run(); if (!success) { return new Response(JSON.stringify({ error: 'Task not found' }), { status: 404, headers: { 'Content-Type': 'application/json' } }); } return new Response(JSON.stringify({ message: 'Task deleted successfully' }), { status: 200, headers: { 'Content-Type': 'application/json' } }); } catch (error) { return new Response(JSON.stringify({ error: 'Failed to delete task' }), { status: 500, headers: { 'Content-Type': 'application/json' } }); } };